package com.lj.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lj.entity.BorrowBooks;
import com.lj.service.AdminBorrowBook;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

/**
 * Servlet implementation class DoAddBorrowBook
 */
@WebServlet("/DoAddBorrowBook")
public class DoAddBorrowBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddBorrowBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// String user_id = request.getParameter("user_id");
		// String book_id = request.getParameter("book_id");
		int borrow_count =  Integer.parseInt(request.getParameter("borrow_count"));
		int total = Integer.parseInt(request.getParameter("total"));
		System.out.println(borrow_count+":"+total);
		HttpSession session = request.getSession();
		if(total-borrow_count<=0){
			request.setAttribute("alt", "书以被借完");
			request.getRequestDispatcher("/WEB-INF/jsp/ExploreBooks.jsp").forward(request, response);
		}
		if((String)session.getAttribute("power")=="1"){
			request.setAttribute("alt", "用户账号借书");
			request.getRequestDispatcher("/WEB-INF/jsp/ExploreBooks.jsp").forward(request, response);
		}
		AdminBorrowBook abb = new AdminBorrowBook();
		BorrowBooks bb =  new BorrowBooks();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Calendar calendar = Calendar.getInstance();
	    String borrowdata = df.format(calendar.getTime());
	    calendar.add(Calendar.DAY_OF_YEAR, +7);
	    String maxstilldata = df.format(calendar.getTime().getTime());
			String user_name = (String) session.getAttribute("username");
			int user_id = (int) session.getAttribute("user_id");
			String book_id = request.getParameter("book_id");
			String book_name = request.getParameter("book_name");
			System.out.println(user_name + user_id+book_id+book_name );
		    bb.setBook_id(Integer.parseInt(book_id));
		    bb.setBook_name(book_name);
		    bb.setUser_id(user_id);
		    bb.setUser_name(user_name);
		    bb.setBorrowdata(borrowdata);
		    bb.setMaxstilldata(maxstilldata);
		    abb.addBorrowBooks(bb);   
		    request.setAttribute("alt", "借书成功");
		    request.getRequestDispatcher("/BooksListServlet").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
